from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
from app.schemas import PriceResponse
from app.services.price_service import price_service
from app.config import settings

router = APIRouter(prefix="/api/prices", tags=["prices"])


@router.get("/")
async def get_all_prices():
    """Get all latest prices"""
    latest_prices = price_service.get_all_latest_prices()
    return {
        symbol: {
            "symbol": price_data.symbol,
            "price": price_data.price,
            "timestamp": price_data.timestamp
        }
        for symbol, price_data in latest_prices.items()
    }


@router.get("/{symbol}")
async def get_price(symbol: str):
    """Get latest price for specific symbol"""
    if symbol not in settings.symbols_list:
        return {"error": f"Symbol {symbol} not supported"}
    
    price_data = price_service.get_latest_price(symbol)
    if not price_data:
        return {"error": f"No price data available for {symbol}"}
    
    return {
        "symbol": price_data.symbol,
        "price": price_data.price,
        "timestamp": price_data.timestamp
    }


@router.get("/supported")
async def get_supported_symbols():
    """Get list of supported symbols"""
    return {"symbols": settings.symbols_list}
